package com.ranshiyan.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.ranshiyan.entity.Comment;
import com.ranshiyan.entity.User;
import com.ranshiyan.service.ArticleService;
import com.ranshiyan.utils.CmsContant;

@RequestMapping("article")
@Controller
public class ArticleController {
	
	@Autowired
	ArticleService articleService;
	
	@RequestMapping("addComment")
	@ResponseBody
	public String addComment(HttpServletRequest request,int id,String content) {
		User loginUser = (User)request.getSession().getAttribute(CmsContant.USER_KEY);
		if(loginUser==null) {
			return "用户尚未登录";
		}
		int result = articleService.addComment(id, content, loginUser.getId());
		if(result<=0) {
			return "不明原因失败，请稍后再试";
		}
		return "success";
	}
	
	//根据文章的id获取评论
	@RequestMapping("listComment")
	public String listComment(HttpServletRequest request,int id,@RequestParam(defaultValue="1")int page) {
		PageInfo<Comment> commentPage = articleService.listComment(id, page);
		request.setAttribute("commentPage", commentPage);
		return "comments";
	}
}
